package com.runen.infinitemovement.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.fragment.MyActivityFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyActivity extends BaseActivity {

    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title_1)
    TextView topTitle1;
    @BindView(R.id.top_title2)
    TextView topTitle2;
    @BindView(R.id.radio_group)
    RadioGroup radioGroup;
    @BindView(R.id.btn_add)
    Button btnAdd;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        initViewPager();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {

                }
            }
        });
    }

    private void initViewPager() {
        setFirst();
        viewPager.setCurrentItem(0);
        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                MyActivityFragment myActivityFragment = new MyActivityFragment(position);
                return myActivityFragment;
            }

            @Override
            public int getCount() {
                return 1;
            }
        });
//        viewPager.setOffscreenPageLimit(3);
        /**
         * ViewPager切换Fragment,RadioGroup做相应变化
         */
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        setFirst();
                        break;
                    case 1:
                        radioGroup.check(R.id.top_title2);
                        setTextColor(topTitle2, topTitle1);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setTextColor(TextView tv1, TextView tv2) {
        tv1.setTextColor(getResources().getColor(R.color.color_333333));
        tv2.setTextColor(getResources().getColor(R.color.color_FF999999));
    }

    private void setFirst() {
        radioGroup.check(R.id.top_title_1);
        setTextColor(topTitle1, topTitle2);
    }

    @OnClick({R.id.top_btn_left, R.id.btn_add, R.id.top_title_1, R.id.top_title2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.top_btn_left:
                finish();
                break;
            case R.id.btn_add:
                Intent intent = new Intent(this, PublishStatusActivity.class);
                intent.putExtra("PUBLISH_FLAG", 0);
                startActivity(intent);
                break;
            case R.id.top_title_1:
                viewPager.setCurrentItem(0);
                break;
            case R.id.top_title2:
                viewPager.setCurrentItem(1);
                break;
        }
    }
}
